import { getUrlName } from "@/router";
import { Pages } from "@/router/types/pages";
import { useUserStore } from "@/store/modules/user";

// is iphone x <= iphone
export function isNewIphone(model?: string): boolean {
  if (!model) model = uni.getDeviceInfo()?.model || uni.getSystemInfoSync()?.model;

  model = model.toLowerCase();
  return model.includes('iphone X') || model.includes('iphone x') || model.includes('iphone 1');
}

/** 是否包含中文 */
export function isIncludeChinese(str: string) {
  return /.*[\u4e00-\u9fa5]+.*$/.test(str);
}

/** 是否为JSON字符串 */
export function isJSON(jsonStr: string) {
  if (typeof jsonStr == 'string') {
    try {
      var obj = JSON.parse(jsonStr);
      if (typeof obj == 'object' && obj) {
        return obj;
      } else {
        return false;
      }
    } catch (e) {
      // console.log('error：' + jsonStr + '!!!' + e);
      return false;
    }
  }
}

/**
 * @description 是否已创建智能助手
 * @date 10/03/2025
 * @export
 * @param {() => void} fn
 */
export function isCreatedAI(fn: () => void) {
  const { userInfo } = storeToRefs(useUserStore());
  if (!userInfo.value) {
    navigateTo({
      name: 'login', options: {
        success: () => {
          isCreatedAI(fn)
        }
      }
    });
    return;
  }
  if (userInfo.value?.aiUserAccountBotVo) {
    fn?.()
    return true;
  }

  uni.showModal({
    title: '提示',
    content: '你还未创建智能助手,是否要前往创建？',
    cancelColor: '#969799',
    cancelText: '取消',
    confirmColor: '#2878FF',
    confirmText: '去创建',
    success: (res) => {
      if (res.confirm) {
        navigateTo({ name: 'createAi' });
      }
    },
  });
}

/**
 * @description 获取url中的页面名称
 * @date 18/10/2024
 * @export
 * @param {string} path
 */
export function urlConvertToName(path: string) {
  return (path?.includes('pages') ? getUrlName(path) : path) as keyof typeof Pages;
}
